package com.lap.generator.infrastructure.persistence;

import com.lap.generator.domain.CoreFile;
import com.lap.generator.domain.repository.FileWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.FileUtils;

@Slf4j
public class FileWriteImpl implements FileWriter {
  @Override
  public void write(List<CoreFile> list) {
    if (CollectionUtils.isNotEmpty(list))
      list.forEach(
          file -> {
            try {
              FileUtils.write(
                  new File(file.getPath() + "/" + file.getFileName()),
                  file.getContent(),
                  StandardCharsets.UTF_8);
            } catch (IOException e) {
              log.error(e.getMessage(), e);
            }
          });
  }
}
